#include "State.h"

LevelSelectState::LevelSelectState(RenderWindow* window) : State(window, LEVEL_SELECT) {
    stateId   = LEVEL_SELECT;
    nextState = LEVEL_SELECT;

    background = new Sprite();
    background->SetImage(imageManager.getImage(guiXml->Attribute("background")));

    TiXmlElement* titleXml = getChildElementbyName("title", guiXml);    
    title = new String(titleXml->FirstChildElement("content")->GetText());
    title->SetColor(getColorFromXml(titleXml->FirstChildElement("color")));
    title->SetPosition(Chameleon(titleXml->FirstChildElement("position")->Attribute("x")),
                       Chameleon(titleXml->FirstChildElement("position")->Attribute("y")));
    title->SetSize(Chameleon(titleXml->Attribute("size")));

    // Initiate list of levels
    levelNames = getDirContents(LEVELS_DIR);

	// Create visible list of levels
    TiXmlElement* levelXml    = getChildElementbyName("level", guiXml);
    TiXmlElement* levelPosXml = levelXml->FirstChildElement("position");

    for (int i = 0, y = Chameleon(levelPosXml->Attribute("y"));
         i < levelNames.size();
         i++, y += Chameleon(levelPosXml->Attribute("vspace"))) {
        String level(levelNames.at(i));
        level.SetColor(getColorFromXml(levelXml->FirstChildElement("color")));
        level.SetPosition(Chameleon(levelPosXml->Attribute("x")), y);
        level.SetSize(Chameleon(levelXml->Attribute("size")));

        levelTitles.push_back(level);
    }

    selected = 0;
    selectItem(selected);
}

void LevelSelectState::onEvent(Event event) {
    // Close window; exit
    if (event.Type == Event::Closed) {
        window->Close();
    }

    if (event.Type == Event::KeyPressed) {
        switch (event.Key.Code) {
            case Key::Escape:
                nextState = MAIN_MENU;
                break;

            case Key::Down:
                if (selected < levelTitles.size() - 1) {
                    selectItem(++selected);
                }
                break;

            case Key::Up:
                if (selected > 0) {
                    selectItem(--selected);
                }
                break;

            case Key::Space:
                nextState = LEVEL;
                break;
        }
    }
}

void LevelSelectState::onHold() {
    // TODO: implement teh user-friendlies
}

void LevelSelectState::listLevels() {
    for (int i = 0; i < levelTitles.size(); i++) {
        window->Draw(levelTitles.at(i));
    }
}

void LevelSelectState::selectItem(int itemIndex) {
    TiXmlElement* levelXml = getChildElementbyName("level", guiXml);
    TiXmlElement* selectedLevelXml = getChildElementbyName("selectedLevel", guiXml);

    for (int i = 0; i < levelTitles.size(); i++) {
        if (i == itemIndex) {
            levelTitles.at(i).SetColor(getColorFromXml(selectedLevelXml->FirstChildElement("color")));
        } else {
            levelTitles.at(i).SetColor(getColorFromXml(levelXml->FirstChildElement("color")));
        }
    }
}

void LevelSelectState::render() {
    // Clear screen
    window->Clear();

    window->Draw(*background);
    window->Draw(*title);
    listLevels();

    // Display framerate
    if (SHOW_FRAMERATE) {
        framerate->SetText(getFramerate());
        window->Draw(*framerate);
    }
    
    // Finally, display the rendered frame on screen
    window->Display();
}

string LevelSelectState::getSelectedLevel() {
    return levelNames.at(selected);
}

LevelSelectState::~LevelSelectState() {
    delete background;
    delete title;
}
